<%
	String error = null;
	String[] parse = null;
	String[] jarName = null;
	String[] fileName = null;
	java.util.ArrayList list = null;
	java.util.ArrayList methods = null;
	Class c = null;
	if (request.getParameter("class") != null) {
		// Step 3. - Class has been selected, introspect and populate methods (close window)
		fileName = (String[])session.getAttribute("deployFiles");
		java.io.File[] files = new java.io.File[fileName.length];
		for (int i = 0; i < fileName.length; i++) {
			files[i] = new java.io.File(com.googlecode.jwsm.Uploader.directory, fileName[i]);
		}
		try {
			com.googlecode.jwsm.JarClassLoader classLoader = new com.googlecode.jwsm.JarClassLoader(files, Thread.currentThread().getContextClassLoader());
			c = classLoader.loadClass(request.getParameter("class"));
			methods = com.googlecode.jwsm.Service.getValidMethods(c);
		} catch(NoClassDefFoundError exc) {
			error = "&#160;Missing Class:<br/>&#160;&#160;&#160;&#160;" + exc.getMessage().replace('/', '.');
		}
	} else if (request.getParameter("jars") != null) {
		// Step 1. - List all the JARs
		parse = request.getParameter("jars").split("\\|");
		jarName = new String[parse.length - 1];
		fileName = new String[parse.length - 1];
		for (int i = 1; i < parse.length; i++) {
			String[] split = parse[i].split(":");
			jarName[i - 1] = split[0];
			fileName[i - 1] = split[1];
		}
		session.setAttribute("deployFiles", fileName);
	} else if (request.getParameter("file") != null) {
		// Step 2. - List all classes within chosen JAR
		java.io.File file;
		file = new java.io.File(com.googlecode.jwsm.Uploader.directory, request.getParameter("file"));
		java.util.jar.JarFile jar = new java.util.jar.JarFile(file);
		java.util.Enumeration entries = jar.entries();
		java.util.jar.JarEntry entry;
		list = new java.util.ArrayList();
		while (entries.hasMoreElements()) {
			entry = (java.util.jar.JarEntry)entries.nextElement();
			if (entry.getName().endsWith(".class")) {
				list.add(entry.getName().substring(0, entry.getName().length() - 6).replaceAll("/", "."));
			}
		}
	}
%>
<html>
<head>
<title>Choose Main Class - Java Web Service Manager</title>
<%@ include file="css.jsp" %>
<style type="text/css">
	body {
		margin: 0px;
		font-family: sans-serif;
	}
	
	.menu {
		font-size: 10pt;
		font-weight: bold;
		color: white;
		text-decoration: none;
	}
	
	.menu:hover {
		text-decoration: underline;
	}
	
	.table {
		border: 1px solid #000000;
	}
</style>
</head>
<body>
<table width="100%" cellpadding="5" cellspacing="0" border="0" bgcolor="<%= CSSDefaults.BG_COLOR %>">
<tr><td style="font-size: 16pt; font-weight: bold; color: white;">Choose Main Class</td></tr>
</table>
<br/>
<% if (error != null) {%>
	<b style="color: red;"><%= error %></b>
	<br/><br/>
	&#160;Please correct this error and try again
	<br/><br/>
	<center><input type="button" onclick="window.close();" value="Close"/></center>
<% } else if (request.getParameter("class") != null) { %>
	<script language="JavaScript">
		window.opener.setMainClass('<%= request.getParameter("class") %>');
		window.opener.clearMethods();
		<% for (int i = 0; i < methods.size(); i++) { %>
			<%
				java.lang.reflect.Method method = (java.lang.reflect.Method)methods.get(i);
				String incomingXSLT = null;
				String outgoingXSLT = null;
				if (method.getParameterTypes().length > 0) {
					incomingXSLT = "'<default>'";
				}
				if (method.getReturnType() != void.class) {
					outgoingXSLT = "'<default>'";
				}
			%>
			window.opener.addMethod('<%= method.getName() %>', <%= outgoingXSLT %>, <%= incomingXSLT %>);
		<% } %>
		window.close();
		<% if (c.isInterface()) { %>
			alert('You have chosen an interface. A custom instantiation must be present for a successful deployment.');
			window.opener.requireCustomInstantiation(true);
		<% } else { %>
		<%
				try {
					c.newInstance();
					%>window.opener.requireCustomInstantiation(false);<%
				} catch(InstantiationException exc) {
					com.googlecode.jwsm.Log.get().log(java.util.logging.Level.SEVERE, "Exception occurred while instantiating class", exc);
					%>alert('<%= c.getName() %> failed instantiation with a default constructor.\n\nYou may need to specify a custom instantiation for this to work properly.');
					window.opener.requireCustomInstantiation(true);<% 
				} catch(Throwable t) {
					com.googlecode.jwsm.Log.get().log(java.util.logging.Level.SEVERE, "Exception occurred while instantiating class", t);
					String localized = t.getLocalizedMessage();
					if (localized != null) {
						localized = localized.replaceAll("\n", "");
						localized = localized.replaceAll("\r", "");
						localized = localized.replaceAll("'", "\\'");
					}
					%>alert('An exception occurred while attempting to instantiate this object:\n\t<%= localized %>');
					window.opener.requireCustomInstantiation(true);<%
				}
			}
		%>
	</script>
<% } else if (request.getParameter("jars") != null) { %>
	&#160;<b>Choose the main JAR:</b>
	<ul>
	<% for (int i = 0; i < jarName.length; i++) {%>
		<li><a href="?jar=<%= jarName[i] %>&file=<%= fileName[i] %>"><%= jarName[i] %></a></li>
	<% } %>
	</ul>
<% } else if (request.getParameter("file") != null) { %>
	&#160;<b><%= request.getParameter("jar") %> contents:</b>
	<ul>
	<% for (int i = 0; i < list.size(); i++) { %>
		<!-- <li><a href="javascript:window.opener.setMainClass('<%= list.get(i) %>'); window.close();"><%= list.get(i) %></a></li> -->
		<li><a href="?class=<%= list.get(i) %>&file=<%= request.getParameter("file") %>"><%= list.get(i) %></a></li>
	<% } %>
	</ul>
<% } %>
</body>
</html>